package com.imddy.spbatch001.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

//@Configuration
public class PrimaryDataSourceConfiguration {

    /**
     * @Author: xiongwei.dai
     * @Description 获取一级的属性
     * @Date 2023/04/04 16:48
     * @return org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
     */
    @Primary
    @Bean("primaryDataSourceProperties")
    @ConfigurationProperties("spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * @Author: xiongwei.dai
     * @Description 获取下一级的属性(hikari)并创建数据源
     * @Date 2023/04/04 16:48
     * @return com.zaxxer.hikari.HikariDataSource
     */
    @Primary
    @Bean("primaryDataSource")
    @Qualifier(value = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary.hikari")
    public HikariDataSource primaryDataSource() {
        return primaryDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    /**
     * @Author: xiongwei.dai
     * @Description 配置事务管理器
     * @Date 2023/04/04 16:48
     * @param dataSource
     * @return org.springframework.transaction.PlatformTransactionManager
     */
//    @Primary
//    @Bean(name = "primaryTransactionManager")
//    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }


    /**
     * @Author: xiongwei.dai
     * @Description 创建JDBC模板
     * @Date 2023/04/04 16:48
     * @param dataSource
     * @return org.springframework.jdbc.core.JdbcTemplate
     */
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


}
